package com.diycode.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diycode.entity.Users;
import com.diycode.mapper.UsersMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diycode.utils.QueryWrapperUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author diycode
 */
@Service
public class UsersService extends ServiceImpl<UsersMapper, Users>{
    @Resource
    UsersMapper mapper;

    //删除
    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    //批量删除
    public int delete(String ids) {
        return mapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    //根据id查询
    public Users detail(Integer id) {
        return mapper.selectById(id);
    }

    //查询
    public PageInfo<Users> query(Users entity){
        if (entity != null && entity.getPage() != null) {
            PageHelper.startPage(entity.getPage(), entity.getLimit());
        }
        QueryWrapper<Users> queryWrapper= QueryWrapperUtil.build(entity);
        return new PageInfo<>(mapper.selectList(queryWrapper));
    }

    public Users getByLogin(String userName, String userPwd) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Users::getUserName, userName).eq(Users::getPassWord, userPwd);
        return mapper.selectOne(queryWrapper);
    }
}
